<template>
    <div>
        <div>
            <el-button type="danger" size="large" @click="AddPurInfo" :icon="SuccessFilled">保存</el-button>
            <br>
        </div>
        <el-form :inline="true" :model="purchaseForm" class="demo-form-inline" style="height: 80%;">

            <el-form-item label="转钟日期:">
                <el-date-picker v-model="purchaseForm.ConversionDate" type="date" aria-label="选择日期" style="width: 200px"
                    value-format="YYYY-MM-DD" />
            </el-form-item>

        </el-form>
        <el-table :data="rowList" style="width: 100%;" border :scrollbar-always-on="true">
            <el-table-column label="序号" fixed>
                <template v-slot="scope">
                    1
                </template>
            </el-table-column>
            <el-table-column label="育成羊耳号" prop="Earbugles">
                <span>
                    <el-select v-model="purchaseForm.RaisedSheep" style="width: 200px;" @change="ChangData">
                        <el-option v-for="item in HYList" :key="item.id" :label="item.earNumber"
                            :value="item.earNumber" />
                    </el-select>

                </span>
            </el-table-column>
            <el-table-column label="转入栋舍" prop="CottageName">
                <el-select v-model="purchaseForm.ShiftCottage" style="width: 200px;" @change="GetField">
                    <el-option v-for="item in Cottage" :key="item.Id" :label="item.Name" :value="item.Name" />
                </el-select>
            </el-table-column>
            <el-table-column label="转入栏位" prop="FieldName">
                <span>
                    <el-select v-model="purchaseForm.ShiftField" style="width: 200px;">
                        <el-option v-for="item in Field" :key="item.Id" :label="item.Name" :value="item.Name" />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column label="栋舍" prop="CottageName">
                {{ purchaseForm.AtPresentCottage }}
            </el-table-column>
            <el-table-column label="栏位" prop="FieldName">
                <span>
                    {{ purchaseForm.AtPresentField }}
                </span>
            </el-table-column>
            <el-table-column label="性别" prop="PurSex">
                {{ purchaseForm.Sex ? "公" : "母" }}
            </el-table-column>
            <el-table-column label="转钟日龄" prop="GoodName">
                <span>
                    <el-input v-model="purchaseForm.TransplantingAge" style="width: 250px;"></el-input>
                </span>
            </el-table-column>
        </el-table>
    </div>
</template>
<script setup lang="ts">
import http from '@/http';
import { ref, reactive, inject, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { AxiosStatic } from 'axios';
import {
    Plus,
    Search,
    Delete,
    DocumentChecked,
    Back,
    Warning,
    SuccessFilled,
    Stamp
} from '@element-plus/icons-vue'

import { useRoute, useRouter } from "vue-router"
const Router = useRouter();
//采购添加表单
const purchaseForm = reactive({
    SubcultivationId: "0",//转种编号
    RaisedSheep: "",//育成羊耳号
    ShiftCottage: "",//转入栋舍
    ShiftField: "",//转入栏位
    AtPresentCottage: "",//当前栋舍
    AtPresentField: "",//当前栏位
    Sex: true,//性别
    TransplantingAge: "",//转钟日
    ConversionDate: ""//转钟日期
})

const rowList = ref([]);

const newRow = reactive({
    SubcultivationId: "",//转种编号
    RaisedSheep: "",//育成羊耳号
    ShiftCottage: "",//转入栋舍
    ShiftField: "",//转入栏位
    AtPresentCottage: "",//当前栋舍
    AtPresentField: "",//当前栏位
    Sex: true,//性别
    TransplantingAge: "",//转钟日
    ConversionDate: ""//转钟日期
});
//新增行
const addFormInfo = () => {
    purchaseForm.SubcultivationId = "";
    purchaseForm.RaisedSheep = "";
    purchaseForm.ShiftCottage = "";
    purchaseForm.ShiftField = "";
    purchaseForm.AtPresentCottage = "";
    purchaseForm.AtPresentField = "";
    purchaseForm.Sex = true;
    purchaseForm.TransplantingAge = "";
    purchaseForm.ConversionDate = "";
    rowList.value.push(newRow);
}


//栋舍集合
const Cottage = ref([]);
//栋舍下拉方法
const GetCooter = () => {

    http.get("OrganizationRead/api/Cottage/HandleGetCot").then(res => {
        Cottage.value = res.data;
    })
}
//栏位集合
const Field = ref([]);
//栏位下拉方法
const GetField = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + purchaseForm.ShiftCottage).then(res => {
        Field.value = res.data;
    })
}

//耳号
const Earnumber = ref([]);
//耳号
const GetEarnumber = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionEard").then(res => {
        Earnumber.value = res.data;
    })
}


const UppType = (EH: string) => {
    http.put("Mroute/api/SheepFile/SheepFileConditionUpdateprogeny?card=" + EH).then(res => {
        console.log(res.data)
    })
}


//羊只采购添加
const AddPurInfo = () => {
    purchaseForm.SubcultivationId = "0";
    http.post("Organization/api/Subcultivation/AddSubcultivation", purchaseForm).then(res => {
        if (res.data > 0) {
            ElMessage({
                type: 'success',
                message: '添加成功!',
                duration: 2000
            })


            UppType(purchaseForm.RaisedSheep);

            Router.push("Subcultivation");
        }
        else if (res.data == -1) {
            ElMessage({
                type: 'error',
                message: '添加失败，耳号不能重复!',
                duration: 2000
            })
            return;
        }
        else {
            ElMessage({
                type: 'error',
                message: '添加失败!',
                duration: 2000
            })
            return;
        }
    })


}

const HYList = ref([]);
const GetHYLidt = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionarDescendantfile").then(x => {
        console.log(x.data)
        HYList.value = x.data;
    })
}
const ChangData = () => {
    HYList.value.forEach(x => {
        if (x.earNumber == purchaseForm.RaisedSheep) {

            purchaseForm.AtPresentCottage = x.cottage;
            purchaseForm.AtPresentField = x.field;
            if (x.sex == 1) {
                purchaseForm.Sex = true;
            }
            else {
                purchaseForm.Sex = false;
            }
        }
    });
}
//栏位集合
const Field1 = ref([]);
//栏位下拉方法
const GetField1 = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + purchaseForm.AtPresentCottage).then(res => {
        Field1.value = res.data;
    })
}
onMounted(() => {
    GetHYLidt();
    GetCooter();
    addFormInfo();
    GetEarnumber();
})

</script>